package com.hjq.www.View;
import com.hjq.www.Dao.BreederDao;
import com.hjq.www.Dao.Impl.BreederDaoImpl;
import com.hjq.www.utils.JDBCUtils;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.Vector;

/**
 * Create with IntelliJ IDEA
 *
 * @author: hjq
 * @Date:
 * @Description:
 */
public class BreederView extends JFrame{
    Connection conn=null;
    PreparedStatement ps=null;
    ResultSet rs=null;
    private JFrame frame=null;
    private JTable table=null;
    private JScrollPane jsp;
    private DefaultTableModel dtm;
    private DefaultTableCellRenderer dtr;
    private JTextField idField;
    private JTextField usernameField;
    private JTextField passwordField;
    private JTextField genderField;
    private JTextField phoneField;
    private JTextField cardField;
    private Vector v;

    public BreederView() {
        JFrame frame = new JFrame();
        DefaultTableModel dtm=new DefaultTableModel();
        DefaultTableCellRenderer dtr = new DefaultTableCellRenderer();
        dtr.setHorizontalAlignment(JLabel.CENTER);
        JTable table = new JTable(stuTableModel(dtm));
        table.setDefaultRenderer(Object.class, dtr);
        JScrollPane jsp = new JScrollPane();
        jsp.setViewportView(table);
        frame.add(jsp);
        frame.setBounds(260, 30, 1000, 150);
        JPanel panel = new JPanel();
        frame.add(panel, BorderLayout.NORTH);
        JPanel Jpanel = new JPanel();
        JButton backButton = new JButton("返回");
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MainView().init();
                dispose();
            }
        });
        Jpanel.add(backButton);
        JButton resetButton=new JButton("刷新");
        resetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new BreederView();
            }
        });
        Jpanel.add(resetButton);
        frame.setTitle("饲养员信息表");
        frame.add(Jpanel,BorderLayout.SOUTH);
        frame.setVisible(true);
    }

    public void BreederMaster(){
        JLabel idLabel = new JLabel("序    号: ");
        idField = new JTextField(15);
        JLabel usernameLabel = new JLabel("用 户 名: ");
        usernameField = new JTextField(15);
        JLabel passwordLabel = new JLabel("密    码: ");
        passwordField = new JTextField(15);
        JLabel genderLabel = new JLabel("性    别: ");
        genderField = new JTextField(15);
        JLabel phoneLabel = new JLabel("联系电话: ");
        phoneField = new JTextField(15);
        JLabel cardLabel = new JLabel("身份证号: ");
        cardField = new JTextField(15);
        BreederDao br=new BreederDaoImpl();
        Container container = getContentPane();
        setBounds(520, 180, 500, 650);
        JButton addButton = new JButton("添加");
        JButton updateButton = new JButton("修改");
        JButton searchButton = new JButton("查询");
        JButton deleteButton = new JButton("删除");
        JButton resetButton = new JButton("重置");
        //添加一块桌布
        container.setLayout(new BorderLayout());
        JPanel root = new JPanel();
        root.setLayout(null);
        Font f = new Font("宋体", Font.PLAIN, 25);
        idLabel.setBounds(10,10,130,60);
        idField.setBounds(130,23,280,40);
        usernameLabel.setBounds(10, 80, 130, 60);
        usernameField.setBounds(130, 93, 280, 40);
        passwordLabel.setBounds(10, 150, 130, 60);
        passwordField.setBounds(130, 163, 280, 40);
        genderLabel.setBounds(10, 220, 130, 60);
        genderField.setBounds(130, 233, 280, 40);
        phoneLabel.setBounds(10, 290, 130, 60);
        phoneField.setBounds(130, 303, 280, 40);
        cardLabel.setBounds(10,360,130,60);
        cardField.setBounds(130,373,280,40);
        idLabel.setFont(f);
        usernameLabel.setFont(f);
        passwordLabel.setFont(f);
        genderLabel.setFont(f);
        phoneLabel.setFont(f);
        cardLabel.setFont(f);
        root.add(idLabel);
        root.add(idField);
        root.add(usernameLabel);
        root.add(usernameField);
        root.add(passwordLabel);
        root.add(passwordField);
        root.add(genderLabel);
        root.add(genderField);
        root.add(phoneLabel);
        root.add(phoneField);
        root.add(cardLabel);
        root.add(cardField);
        addButton.setBounds(30,450,80,50);
        updateButton.setBounds(140,450,80,50);
        searchButton.setBounds(250,450,80,50);
        deleteButton.setBounds(360,450,80,50);
        resetButton.setBounds(200,520,80,50);
        setTitle("饲养员信息操作");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setVisible(true);
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                br.add(idField, usernameField, passwordField, genderField, phoneField, cardField);
            }
        });
        root.add(addButton);
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                br.delete(idField);
            }
        });
        root.add(deleteButton);
        updateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                update(idField, usernameField, passwordField, genderField, phoneField, cardField);
            }
        });
        root.add(updateButton);
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                br.search(idField,usernameField, passwordField, genderField, phoneField, cardField);
            }
        });
        root.add(searchButton);
        resetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                br.reset(idField,usernameField, passwordField, genderField, phoneField, cardField);
            }
        });
        root.add(resetButton);
        container.add(root);
    }
    /**
     * 使用DefaultTableModel导入表中数据
     * @param dtm
     * @return
     */
    private DefaultTableModel stuTableModel(DefaultTableModel dtm) {
        Statement st = null;
        ResultSet rs = null;
        String[] stu = {"序号", "用户名", "密码", "性别", "电话", "身份证号"};
        dtm.setColumnIdentifiers(stu);
        Vector v = null;
        try {
            conn = JDBCUtils.getConnection();
            st = conn.createStatement();
            String sql = "select * from breeder";
            rs = st.executeQuery(sql);
            while (rs.next()) {
                v = new Vector();
                String sno = rs.getString("id");
                String name = rs.getString("username");
                String sex = rs.getString("password");
                String birthday = rs.getString("gender");
                String phone = rs.getString("phone");
                String address = rs.getString("idnum");
                v.add(sno);
                v.add(name);
                v.add(sex);
                v.add(birthday);
                v.add(phone);
                v.add(address);
                dtm.addRow(v);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            try{
                if(conn!=null){
                    conn.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return dtm;
    }

    public void update(JTextField idField,JTextField nameField,JTextField passwordField,JTextField genderField,JTextField phoneField,JTextField cardField) {
        try {
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);
            String sql = "update breeder set id=?,username=?,password=?,gender=?,phone=?,idnum=? where id=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, idField.getText());
            ps.setString(2, nameField.getText());
            ps.setString(3, passwordField.getText());
            ps.setString(4, genderField.getText());
            ps.setString(5, phoneField.getText());
            ps.setString(6, cardField.getText());
            ps.setString(7,idField.getText());
            if(ps.executeUpdate()==1) {
                JOptionPane.showMessageDialog(null, "修改成功");
            }else{JOptionPane.showMessageDialog(null, "修改失败");}
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null,"修改失败");
            e.printStackTrace();

        } finally {
            JDBCUtils.close(conn,ps);
        }
    }

    public static void main(String[] args) {
        try {
            new BreederView().BreederMaster();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }




    /**
     * 重置
     * @param idField
     * @param nameField
     * @param passwordField
     * @param genderField
     * @param phoneField
     * @param cardField
     */
    private void reset(JTextField idField,JTextField nameField,JTextField passwordField,JTextField genderField,JTextField phoneField,JTextField cardField) {
        idField.setText("");
        nameField.setText("");
        passwordField.setText("");
        genderField.setText("");
        phoneField.setText("");
        cardField.setText("");
    }
}
